package com.dmbjz.coredemo.event;

import org.springframework.context.ApplicationEvent;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.context.ApplicationEventPublisherAware;
import org.springframework.stereotype.Service;

/** 创建事件发布器，一般来说一个项目只需要一个事件发布器即可 */
@Service
public class EventPublisher implements ApplicationEventPublisherAware {

    ApplicationEventPublisher applicationEventPublisher;


    /**
     * 发布事件
     *
     * @param event 需要发布的事件
     */
    public void sendEvent(ApplicationEvent event) {
        //调用底层API发布事件
        applicationEventPublisher.publishEvent(event);
    }


    /**
     * 项目启动时自动调用，把真正发布事件的底层组件 ApplicationEventPublisher 进行注入
     */
    @Override
    public void setApplicationEventPublisher(ApplicationEventPublisher applicationEventPublisher) {
        this.applicationEventPublisher = applicationEventPublisher;
    }


}
